# sendmail.py 
# sendmail.py : the function is to send mail, the from_mail name is replace by 'Smart-Backup'.
#
#
# Copyright from 2012 - 2015
# 
# 1Sep 2012 - Create by Wang Huanhuan

import smtplib
class SendMail:
    def __init__(self,from_mail,psw,to_mail,detail):
        self.from_mail = from_mail    # From Mail Address
        self.psw       = psw          # From Mail Password
        self.to_mail   = to_mail      # To Mail Address
        self.detail    = detail       # The contents of the Mail

    def send_mail(self):
        '''Meet the communication protocol , send e-mail.
        '''       
        sm = "smtp." + self.from_mail[self.from_mail.find("@") + 1 : ]       
        mail_detail = ['From:%s' %'Smart-Backup',
       'To:%s' % self.to_mail,
       'Subject: Log Info \r\n','%s' %self.detail]
        try:
            s = smtplib.SMTP()
            s.connect(sm,25)    # Smtp service port , default is 25
            s.login(self.from_mail,self.psw)  
            s.sendmail(self.from_mail, self.to_mail,'\r\n'.join(mail_detail))
            s.quit()
            print 'Successfully send a mail,from %s to %s' %(self.from_mail,to_mail)
            return True
        except Exception,e:
            print str(e)
            return False

if __name__ == '__main__':
    from_mail = "******"
    psw       = "******"
    to_mail   = "******"
    detail    = "detail"

    send_m = SendMail(from_mail,psw,to_mail,detail)
    send_m.send_mail()
